package org.thot.model.ge 
{
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	/**
	 * ...
	 * @author Thot
	 */
	public class CyclicGameEvent extends GameEvent
	{
		
		protected var m_timer:Timer;
		protected var m_timerEvent:TimerEvent;
		public var allowExternalLaunch:Boolean;
		
		
		public function CyclicGameEvent( p_name:String, p_trigger:String, p_cadency:Number, p_persistance:int = -1 ) 
		{
			super( p_name, p_trigger, p_persistance );
			init( p_cadency );
		}
		
		
		private function init( p_cadency:Number ):void 
		{
			m_timerEvent = null;
			m_timer = new Timer( p_cadency, 0 );
			m_timer.addEventListener(TimerEvent.TIMER, tickHandler );
			allowExternalLaunch = true;
		}
		
		private function tickHandler( e:TimerEvent ):void
		{
			m_timerEvent = e;
			super.exec();
		}
		
		
		/**
		 * désactive l'appel à exec depuis le gameEventPool
		 */
		override public function exec():void
		{
			if ( allowExternalLaunch )
			{
				super.exec();
			}
			else
			{
				return;
			}
		}
		
		
		public function stop():void
		{
			m_timer.stop();
		}
		
		public function start():void
		{
			m_timer.start();
		}
		
		
		public function set cadency( p_cadency:Number ):void
		{
			m_timer.stop();
			m_timer.delay = p_cadency;
			m_timer.start();
		}
		
		public function get cadency():Number
		{
			return m_timer.delay;
		}
		
		
		public function get timerEvent():TimerEvent 
		{
			return m_timerEvent;
		}
		
		
		
		override public function destroy():void
		{
			m_timerEvent = null;
			
			m_timer.stop();
			m_timer.removeEventListener(TimerEvent.TIMER, tickHandler );
			
			m_timer = null;
			
			super.destroy();
		}
		
		
		
	}

}